#include <iostream>
#include <string>
#include <climits> // 用于 INT_MAX
using namespace std;

int main() {
    int n;
    cin >> n;
    string str1, str2;
    cin >> str1 >> str2;

    // 处理 str1 或 str2 为空的情况
    if (str1.empty() || str2.empty()) {
        cout << -1 << endl;
        return 0;
    }

    int min_distance = INT_MAX;
    int last_pos1 = -1; // 记录 str1 最近出现的位置
    int last_pos2 = -1; // 记录 str2 最近出现的位置

    for (int i = 0; i < n; ++i) {
        string tmp;
        cin >> tmp;

        if (tmp == str1) {
            last_pos1 = i;
            if (last_pos2 != -1) {
                min_distance = min(min_distance, last_pos1 - last_pos2);
            }
        } else if (tmp == str2) {
            last_pos2 = i;
            if (last_pos1 != -1) {
                min_distance = min(min_distance, last_pos2 - last_pos1);
            }
        }
    }

    // 检查是否两个字符串都出现过
    if (last_pos1 == -1 || last_pos2 == -1) {
        cout << -1 << endl;
    } else {
        cout << min_distance << endl;
    }

    return 0;
}